package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zwnan
 */
@RestController
@RequestMapping("/admin/storage")
@CrossOrigin
public class StorageController {

    @Autowired
    private DtsStorageService dtsStorageService;
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "添加")
    @RequiresPermissions(value = "{admin:storage:create}")
    @PostMapping("/create")
    public Object create(MultipartFile file) throws Exception {

        //上传文件 并保存
        dtsStorageService.create(file.getBytes(),file.getOriginalFilename(),file.getSize());

        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "查询")
    @RequiresPermissions(value = "{admin:storage:list}")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page
                       , @RequestParam(defaultValue = "20") Integer limit
                       , @RequestParam(defaultValue = "add_time") String sort
                       ,@RequestParam(defaultValue = "desc") String order){

        IPage<DtsStorage> iPage = dtsStorageService.selectPage(page,limit,sort,order);

        long total = iPage.getTotal();
        List<DtsStorage> records = iPage.getRecords();

        Map data = new HashMap();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "删除")
    @RequiresPermissions(value = "{admin:storage:delete}")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsStorage dtsStorage){
        dtsStorageService.delete(dtsStorage);
        return ResponseUtil.ok();
    }
    @RequiresPermissionsDesc(menu = {"系统管理","对象存储"},button = "更新")
    @RequiresPermissions(value = "{admin:storage:update}")
    @PostMapping("/update")
    public Object update(@RequestBody DtsStorage dtsStorage){
        dtsStorageService.update(dtsStorage);
        return ResponseUtil.ok();
    }


}
